//不显示任何错误信息
$.fn.dataTable.ext.errMode = 'none';
//以下为发生错误时的事件处理，如不处理，可不管。
$('#example').on('error.dt', function (e, settings, techNote, message) {
    //这里可以接管错误处理，也可以不做任何处理
    console.log('An error has been reported by DataTables: ', message);
}).DataTable();
var clazz;
var name;
var url;
var type;

var table;
var qqnumcolumsName = [
    {"data": "qqnum", "title": "QQ号码"},
    {"data": "nick", "title": "昵称"},
    {
        "data": "age", "title": "年龄", "render": function (data, type, row, meta) {
            return data + 9;
        }
    },
    {
        "data": "gender", "title": "性别", "render": function (data, type, row, meta) {
            if (data === 0) {
                return '男'
            }
            if (data === 1) {
                return '女'
            }
            return '未知';
        }
    },
    {
        "data": "auth", "title": "角色", "render": function (data, type, row, meta) {
            if (data === 4) {
                return '群主'
            }
            if (data === 2) {
                return '管理员'
            }
            if (data === 1) {
                return '群成员'
            }
            return '未知';
        }
    },
    {"data": "qunnum", "title": "群号码"},
    {"data": "title", "title": "群名称"},
    {
        "title": "操作", "render": function (data, typea, row, meta) {
            var num;
            var t;
            var u;
            var key;
            var clz = clazz;
            if (type === "qun") {

                num = row.qqnum;
                t = "qq";
                u = "/data/qqnum";
                key = u + t + row.qqnum + row.qunnum
            }
            if (type === "qq") {
                num = row.qunnum;
                t = "qun";
                u = "/data/qun";
                key = u + t + row.qunnum + row.qqnum
            }
            if (localStorage.getItem(key)) {
                clz = "btn btn-info";
            }
            return '<button type="button" key=' + key + ' t=' + t + ' n=' + num + ' u=' + u + ' class="btn ' + clz + ' btn-xs" onclick="opennew(this)">' + name + '</button>'
        }
    }
];
$(function () {
    if (!window.localStorage) {
        alert("浏览暂不支持本地缓存，不能记录一点击的条目。请更换浏览器或改为极速模式，建议使用Chrome浏览器");
    }
    // table = createTable(qqnumcolumsName);
    $("#inlineRadio1").click();
    var url = GetUrlParam("u");
    var num = GetUrlParam("n");
    var id = GetUrlParam("t");
    if (url && num && id) {
        $("#" + id).click();
        $("#exampleInputName2").val(num);
        $("#qwjiowwqw").click();
    }
});

function opennew(obj) {

    var u = $(obj).attr("u");
    var n = $(obj).attr("n");
    var t = $(obj).attr("t");
    var key = $(obj).attr("key");
    $(obj).removeClass();
    $(obj).addClass("btn btn-info  btn-xs");
    localStorage.setItem(key, true);
    window.open('http://' + window.location.host + GetUrlRelativePath() + '?u=' + u + '&n=' + n + '&t=' + t, "_blank");
}

function search() {
    var value = $('input:radio[name="radioInline"]:checked').val();
    if (value === "qun") {
        name = '查看所在QQ群';
        clazz = 'btn-danger';
        url = '/data/qun';
        type = "qun";
    }
    if (value === "qq") {
        name = '查看群内成员';
        clazz = 'btn-danger';
        url = '/data/qqnum';
        type = "qq";
    }
    var s = $("#exampleInputName2").val();
    if (s) {
        var data = {"num": s}
        createTable(qqnumcolumsName, url, data);
    }

}


//公共方法，多次初始化调用
function createTable(columns, url, data) {
    if (table) {
        table.destroy();
        table = null;
    }
    table = $("#example").DataTable({
        "paging": false,
        "lengthChange": true,
        // "autoWidth": false,
        "bInfo": false,
        "searching": false,
        "bLengthChange": false,
        "sPaginationType": "full_numbers",
        ajax: {
            url: url,
            data: data,
            "dataSrc": function (json) {
                if (json.code !== '0000') {
                    console.log(json);
                    table.destroy();
                    table = null;
                }
                return json.data;
            }
        },
        //因为需要多次初始化，所以需要设置允许销毁实例
        "destroy": true,
        //列的配置信息通过变量传进来
        "columns": columns,
        language: {
            "sProcessing": "处理中...",
            "sLengthMenu": "显示 _MENU_ 项结果",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "没有数据",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        }
    });
    return table;
}

//paraName 等找参数的名称
function GetUrlParam(paraName) {
    var url = document.location.toString();
    var arrObj = url.split("?");

    if (arrObj.length > 1) {
        var arrPara = arrObj[1].split("&");
        var arr;

        for (var i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split("=");

            if (arr != null && arr[0] == paraName) {
                return arr[1];
            }
        }
        return "";
    }
    else {
        return "";
    }
}

function GetUrlRelativePath() {
    var url = document.location.toString();
    var arrUrl = url.split("//");

    var start = arrUrl[1].indexOf("/");
    var relUrl = arrUrl[1].substring(start);//stop省略，截取从start开始到结尾的所有字符

    if (relUrl.indexOf("?") != -1) {
        relUrl = relUrl.split("?")[0];
    }
    return relUrl;
}
